<?php

namespace Model;

use Lib;

class ChessModel
{

        private $db = null;
        private $table = 'tbchess';
        public function __construct()
        {
            $this->db = Lib\Db\DbModel::init(); // 构造方法，线上自动切换
        }

        /**
         * 根据棋子Id以及版本Id,季度获取棋子信息
         * @param $raceId
         * @param $versionId
         * @return array
         */
        public function getChessInfo($chessId,$where)
        {

            $where['chessId'] = $chessId;
            $race = $this->db->table($this->table)->where($where)->find();
            return $race;
        }

        /**
         * 根据版本Id和季度获取对应的版本棋子
         * @param $versionId
         * @return array
         */
        public function getAllChess($where)
        {

            $race = $this->db->table($this->table)->where($where)->order("chessId","asc")->select();

            return $race;
        }

        //检查TFTID是否已经存在了
        public function checkTFTID($chessId,$where,$TFTID)
        {
            $where['TFTID'] = $TFTID;
            $where['chessId'] = ['!=',$chessId];

            $res = $this->db->table($this->table)->where($where)->find();
            return $res;
        }


        /**
         * 修改棋子信息
         * @param $versionId
         * @param $raceId
         * @param $data
         */
        public function updateChess($chessId,$where,$data)
        {

            $where['chessId'] = $chessId;
            return $this->db->table($this->table)->where($where)->update($data);
        }


        public function insertChess($versionId,$seasonId,$data)
        {
            $arr_keys = array_keys($data[0]);
            array_push($arr_keys,'versionId');
            array_push($arr_keys,'seasonId');
            $sql = 'INSERT INTO '.$this->table .' (' . implode(',' ,$arr_keys) . ') values';
            foreach ($data as &$time){
                $time['versionId'] = $versionId;
                $time['seasonId'] = $seasonId;


                $arr_values = array_values($time);
                $sql.= " ('" . implode("','" ,$arr_values) . "'),";
            }
            $sql = substr($sql ,0 ,-1);


            $result = $this->db->table($this->table)->exec($sql);


            var_dump($result); exit();
        }


    public function insertChessOne($versionId,$seasonId,$data)
    {
        $arr_keys = array_keys($data);
        array_push($arr_keys,'versionId');
        array_push($arr_keys,'seasonId');
        $sql = 'INSERT INTO '.$this->table .' (' . implode(',' ,$arr_keys) . ') value';
        $data['versionId'] = $versionId;
        $data['seasonId'] = $seasonId;
        $arr_values = array_values($data);
        $sql.= " ('" . implode("','" ,$arr_values) . "'),";
        $sql = substr($sql ,0 ,-1);
        $result = $this->db->table($this->table)->exec($sql);
        return $result;
    }
}